package com.weishe.weichar;

import java.util.Date;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class TopWeather {

	public static void main(String[] args) throws Exception {
		String inputPath = "hdfs://192.168.15.10:9000/data";
		String outputPath = "hdfs://192.168.15.10:9000/weatherOut" + (new Date()).getTime();

		// 1、配置文件
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(conf);
		Job job = Job.getInstance(conf);
		job.setJarByClass(TopWeather.class);
		// 2、设置Job

		job.setJobName("top weather");

		job.setMapOutputKeyClass(Weather.class);
		job.setMapOutputValueClass(Text.class);
		
		
		//如果是mapper 和reduce的输出是一样的name就采用这个设置
		//job.setOutputKeyClass(Text.class);
		//job.setOutputValueClass(NullWritable.class);

		job.setMapperClass(WeathMapper.class);
		job.setReducerClass(WeatherReducer.class);

		// 设置分区
		job.setPartitionerClass(WeatherPartitioner.class);
		// 设置排序
		job.setSortComparatorClass(WeatherComparator.class);
		// 设置分组
		job.setGroupingComparatorClass(WeatherGroup.class);

		
		//设置reducer的数量
		job.setNumReduceTasks(3);
		
		FileInputFormat.setInputPaths(job, new Path(inputPath));
		Path op = new Path(outputPath);
		// if (fs.exists(op)) {
		// fs.delete(op, true);
		// }
		FileOutputFormat.setOutputPath(job, op);

		boolean f = job.waitForCompletion(true);
		if (f) {
			System.out.println("job 成功执行");
		}

	}

}
